﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess_Layer;
using Business_Layer;
using System.Collections;
public partial class MyAdmin_NhaXuatBan_AM : System.Web.UI.Page
{
    string errorParam = "Error";
    string errorID = "ErrorID";
    LogAction log = new LogAction();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Phân quyền
        if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenXem)
        {
            // xử lý quyền xem
            ((CommandField)gvDSNhaXuatBan.Columns[0]).ShowDeleteButton = false;
            CommandField dtCmdField = ((CommandField)dtvNhaXuatBan_AM.Fields[dtvNhaXuatBan_AM.Fields.Count - 1]);
            dtCmdField.ShowEditButton = false;
            dtCmdField.ShowInsertButton = false;
            XoaNhieuBUTTON.Visible = false;
        }
        else if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenChinhSua)
        {
            // xử lý quyền chỉnh sửa
        }

        lblThongBaoDTView.Visible = false;
        lblThongBaoGridView.Visible = false;
    }

    protected void gvDSNhaXuatBan_SelectedIndexChanged(object sender, EventArgs e)
    {        
        refreshDetailViewPageIndex();
    }

    protected void dtvNhaXuatBan_AM_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        lblThongBaoDTView.Visible = true;
        string id = e.Keys[0].ToString();

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Cập nhật thành công record có ID là " + id;
            return;
        }

        lblThongBaoDTView.Text = "Cập nhật thất bại record!";
        e.ExceptionHandled = true;

        gvDSNhaXuatBan.DataBind();
        refreshDetailViewPageIndex();  
    }

    protected void dtvNhaXuatBan_AM_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {       
        gvDSNhaXuatBan.DataBind();
        Helper.changeGVPageIndex(gvDSNhaXuatBan, gvDSNhaXuatBan.PageCount - 1);
        gvDSNhaXuatBan.SelectedIndex = gvDSNhaXuatBan.Rows.Count - 1;
        refreshDetailViewPageIndex();
    }

    protected void gvDSNhaXuatBan_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            string undeletedID = e.Keys[0].ToString();
            Response.Write(Helper.CreateMessageBox("Không thể xóa Record có ID " + undeletedID));
            e.ExceptionHandled = true;
        }
        else
        { 
            long id = long.Parse(e.Keys[0].ToString());
            Response.Write(Helper.CreateMessageBox("Đã xóa Record có ID = " + id));
            dtvNhaXuatBan_AM.DataBind();
        }
    }

    protected void XoaNhieuBUTTON_Click(object sender, EventArgs e)
    {
        string ids = "";
        List<long> deletedIDs = new List<long>();
        int lastCellIndex = gvDSNhaXuatBan.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvDSNhaXuatBan.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                long id = long.Parse(r.Cells[1].Text);
                deletedIDs.Add(id);
                ids += id.ToString() + ", ";
            }
        }

        lblThongBaoGridView.Visible = true;
        lblThongBaoGridView.Text = "Xóa thành công record: " + ids.Substring(0, ids.LastIndexOf(", "));
        //log
        log.TenLoaiCapNhat = "Xóa";
        log.Ngay = DateTime.Now;
        log.TenBang = "Bảng Nhà Xuất bản";
        log.TenLoaiDangNhap = "Admin";
        log.TenDangNhap = Helper.getCurrentAdminUserName(Session);
        string logText = "";
        log.DuLieuCu = "record ID:" + ids.Substring(0, ids.LastIndexOf(", "));
        logText = "X";
        log.DuLieuMoi = logText;
        Log_BUS.insert(log);
        //log
        try
        {
            DataAccess_Layer.NhaXuatBan_DAC.delete(deletedIDs);
        }
        catch (System.Exception ex)
        {
            lblThongBaoGridView.Text = "Xóa thất bại!";
        }
        
        gvDSNhaXuatBan.DataBind();
        dtvNhaXuatBan_AM.DataBind();
    }

    protected void gvDSNhaXuatBan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Helper.changeGVPageIndex(gvDSNhaXuatBan, e.NewPageIndex);
        gvDSNhaXuatBan.SelectedIndex = gvDSNhaXuatBan.Rows.Count > 0 ? 0 : -1;
        gvDSNhaXuatBan.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaXuatBan);
    }

    private void refreshDetailViewPageIndex()
    {
        dtvNhaXuatBan_AM.DataBind();
        dtvNhaXuatBan_AM.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaXuatBan);
    }
    protected void gvDSNhaXuatBan_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dtvNhaXuatBan_AM.DataBind();
    }
    protected void gvDSNhaXuatBan_PageIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }
    protected void LQDSHoaDon_AM_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        //log
        try
        {
            log.TenLoaiCapNhat = "Thêm";
            log.Ngay = DateTime.Now;
            log.TenBang = "Bảng xuất bản";
            log.TenLoaiDangNhap = "Adminstator";
            log.TenDangNhap = Helper.getCurrentAdminUserName(Session);

            DataAccess_Layer.NhaXuatBan logO;

            log.DuLieuCu = "X";

            logO = (DataAccess_Layer.NhaXuatBan)e.Result;
            log.DuLieuMoi = "tên nhà xuất bản:" + logO.Ten;
            Log_BUS.insert(log);
        }
        catch { }
        //log
        lblThongBaoDTView.Visible = true;
        DataAccess_Layer.NhaXuatBan newOrder = (DataAccess_Layer.NhaXuatBan)e.Result;

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Thêm thành công record có ID là " + newOrder.ID;
            return;
        }

        lblThongBaoDTView.Text = "Thêm thất bại record!";
        e.ExceptionHandled = true;
    }
    protected void LQDSNhaXuatBan_AM_Updating(object sender, LinqDataSourceUpdateEventArgs e)
    {

    }
}